package modules

import (
	"service/components"

	"gorm.io/gorm"
)

type Department struct {
	gorm.Model
	Name        string `json:"name" gorm:"type:varchar(300);comment:部门名称"`
	MustSign    string `json:"MustSign" gorm:"default:;comment:必须签;column:MustSign"`
	OrSign      string `json:"OrSign" gorm:"default:;comment:或签;column:OrSign"`
	Notify      string `json:"Notify" gorm:"type:varchar(300);comment:通知;column:Notify"`
	Permissions string `json:"permissions" gorm:"type:varchar(300);comment:部门名称"`
}

func (d Department) TableName() string {
	return "department"
}

// CreateDepartmentService 创建部门
func CreateDepartmentService(d Department) error {
	return components.DB.Model(&Department{}).Create(&d).Error
}

// FetchDepartmentService 获取全部部门
func FetchDepartmentService() ([]Department, error) {
	departments := make([]Department, 20)
	err := components.DB.Model(&Department{}).Find(&departments).Error
	return departments, err
}

// FetchDepartmentByNameService 获取某个部门详情
func FetchDepartmentByNameService(name string) (*Department, error) {
	departments := new(Department)
	err := components.DB.Model(&Department{}).
		Where("name = ?", name).
		First(&departments).Error
	return departments, err
}

// FetchDepartmentByIDService 获取某个部门详情
func FetchDepartmentByIDService(d Department) (*Department, error) {
	departments := new(Department)
	err := components.DB.Model(&Department{}).
		Where("id = ?", d.ID).
		First(&departments).Error
	return departments, err
}

// PatchDepartmentService 更新部门
func PatchDepartmentService(d *Department) error {
	return components.DB.Model(&Department{}).Select("*").
		Where("id = ?", d.ID).
		Updates(d).Error
}
